<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/15
 * Time: 9:13
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Banner;
use app\api\model\Message;
use app\api\model\Tour;
use app\api\model\Method;
use app\api\model\Hotel;
use app\api\model\Food;
class Index extends Nocommon
{
    /*
     * 首页接口
     */
    public function index()
    {
        if(Request::instance()->isPost()){
            $post = input('param.');
            $uid = input('uid','','intval');
            //获取banner
            $banner = new Banner();
            $data['banner'] = $banner->banner();
            //查詢精選推薦
            $tour = Db::name('tours')->field('tour_id as id ,tour_name as name,tour_covers as covers,is_stick,stick_time')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('is_pass',1)
                    ->where('is_tuijian',1)
                    ->order('is_stick desc,stick_time desc')
                    ->limit(4)
                    ->select();
            foreach ($tour as $k=>$v){
                 $tour[$k]['covers']  = addWaterByField($v['covers']);
                 //处理好评率
                 $tour[$k]['appraise'] = model('AppraiseModel')->getAppRate(1,$v['id']);
                 $tour[$k]['status'] = 1;
            }      

            $hotel = Db::name('hotel')
            ->field('hotel_id as id,hotel_name as name,hotel_covers as covers,is_stick,stick_time')
            ->where('is_del',0)
            ->where('is_pass',1)
            ->where('is_display',0)
            ->where('is_tuijian',1)
             ->order('is_stick desc,stick_time desc')
            ->limit(4)
            ->select();
            foreach ($hotel as $key => $value) {
                $hotel[$key]['covers']  = addWaterByField($value['covers']);
                //处理好评率
                $hotel[$key]['status'] = 2;
                $hotel[$key]['appraise'] = model('AppraiseModel')->getAppRate(2,$value['id']);
                
            }

            $food =  Db::name('food_store')
                ->field('food_store_id as id,food_store_cover as covers,food_store_name as name,is_stick,stick_time')
                ->where('is_del',0)
                ->where('is_pass',1)
                ->where('is_tuijian',1)
                ->order('is_stick desc,stick_time desc')
                ->limit(4)
                ->select();
            foreach($food as $k=>$v){
                 $food[$k]['covers']  = addWaterByField($v['covers']);
                 $food[$k]['appraise'] = -1;
                 $food[$k]['status'] = 3;
            }
            $c = array_merge($tour,$hotel,$food);
            if(!empty($c)){
                $last = array_column($c, 'is_stick');
                $last_names = array_column($c,'stick_time');
                array_multisort($last,SORT_DESC,$last_names,SORT_DESC,$c);
            }
            $data['list'] = $c;

            //查询用户是否有未读信息
            if(empty($uid)){
                $data['is_read'] = 0;
            }else{
                $is_read = new Message();
                $read = $is_read->is_read($uid);
                if(empty($read)){
                    $data['is_read'] = 0;
                }else{
                    $data['is_read'] = 1;
                }
                //查询是否有已读维度的消息
                $order_msg = Db::name('message')
                    ->where('receive_user_id',$uid)
                    ->where('is_read',0)
                    ->where('msg_type',2)
                    ->select();
                if($order_msg){
                    $data['order_msg'] = 1;
                }else{
                    $data['order_msg'] = 0;
                }
                //系统消息
                $system_msg = Db::name('message')
                    ->where('receive_user_id',$uid)
                    ->where('is_read',0)
                    ->where('msg_type',1)
                    ->select();
                if($system_msg){
                    $data['system_msg'] = 1;
                }else{
                    $data['system_msg'] = 0;
                }

                //互动消息
                $h_msg1 =  Db::name("comment_message")->where("uid",$post['uid'])->where("is_read",0)->select();

                $h_msg2 =  Db::name("comment_method_message")->where('uid',$post['uid'])->where('is_read',0)->select();

                $h_msg3 = Db::name("log_like")->where("send_user",$post['uid'])->where("is_read",0)->select();
                if($h_msg1 || $h_msg2 || $h_msg3){
                    $data['h_msg'] = 1;
                }else{
                    $data['h_msg'] = 0;
                }

            }
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 玩啥吃啥住啥接口
     */
    public function play()
    {
        $post = input('param.');
        $page = $this->p();
        if($post['type']==1){
            //获取玩啥的数据
            $play = new Tour();
            $data = $play->play($page);
            $this->apiReturn('200','成功',$data);
        }
        if($post['type']==2){
            $live = new Hotel();
            $data = $live->live($page,$post['latitude'],$post['longitude']);
            $this->apiReturn('200','成功',$data);
        }
        if($post['type']==3)
        {
            $eat = new Food();
            $data = $eat->eat($page,$post['latitude'],$post['longitude']);
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 启动页
     */
    public function start()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('app_start')->field('id,app_img,type')->select();
            $data = [];
            foreach($res as $k=>$v){
                if($v['type']==1){
                    array_push($data,$v['app_img']);
                }
                if($v['type']==2){
                    array_push($data,$v['app_img']);
                }
            }
            $data1['all'] = addWaterByField($data[0]);
            $data1['small'] = addWaterByField($data[1]);
            $this->apiReturn('200','成功',$data1);
        }
    }
}

